import re

from rest_framework.response import Response

from users.models import User
from rest_framework import status


def jwt_response_payload_handler(token, user=None, request=None):
    #  jwt 的token
    #  user 就是认证之后的用户
    return {
        'token': token,
        'user_id': user.id,
        'username': user.username,

    }


from django.contrib.auth.backends import ModelBackend

"""
登陆采用的就是jwt的认证方式，jwt的认证是在rest_framework的基础上实现的
    jwt 其实就是用　rest_framework 的认证　返回的就是　token
    jwt 其实就是用　rest_framework 的认证  rest_framework 的认证是根据用户名名来判断的，满足不了需求，所有需要重写里面的方法
需要判断用户输入的是手机号还是用户名
"""


def get_user(username):
    """
    抽取封装
    将要抽取的代码原封不动的放到一个函数中，
    抽取的代码的变量　定义为函数的参数
    抽取的函数替换源代码
    :param username:
    :return:
    """
    try:
        if re.match(r'1[345789]\d{9}', username):
            count = User.objects.filter(username=username).count()
            if count != 0:
                user = User.objects.get(username=username)
            else:
                #  手机号登陆
                user = User.objects.get(mobile=username)
                # if not user:
                #     user = User.objects.get(username=username)
        else:
            #  用户名登陆
            user = User.objects.get(username=username)
    except User.DoesNotExist:
        return None
    return user


class MobileUsernameModelBackend(ModelBackend):
    """
    继承ModelBackend　重写里面登陆认证的方法，添加新的认证方法

    """

    def authenticate(self, request, username=None, password=None, **kwargs):
        # 根据username 查询用户信息
        #  username 有可能是手机号，还有可能是用户名
        # try:
        #     if re.match(r'1[345789]\d{9}', username):
        #         #  手机号登陆
        #         user = User.objects.get(mobile=username)
        #         # if not user:
        #         #     user = User.objects.get(username=username)
        #     else:
        #         #  用户名登陆
        #         user = User.objects.get(username=username)
        # except User.DoesNotExist:
        #     return None
        user = get_user(username)

        # 校验用户密码
        if user is not None and user.check_password(password):
            return user
        # else:
        #     return BaseException


class SettingsBackend(object):
    """
    另外一种方式实现用户登陆的校验
    Authenticate against the settings ADMIN_LOGIN and ADMIN_PASSWORD.

    Use the login name and a hash of the password. For example:

    ADMIN_LOGIN = 'admin'
    ADMIN_PASSWORD = 'pbkdf2_sha256$30000$Vo0VlMnkR4Bk$qEvtdyZRWTcOsCnI/oQ7fVOu1XAURIZYoOZ3iq8Dr4M='
    使用自定义登陆校验方法
    需要实现　authenticate　　和　get_user　两个方法
    """

    def authenticate(self, request, username=None, password=None):
        # 根据username 查询用户信息
        #  username 有可能是手机号，还有可能是用户名
        try:
            if re.match(r'1[345789]\d{9}', username):
                #  手机号登陆
                user = User.objects.get(mobile=username)
                # if not user:
                #     user = User.objects.get(username=username)
            else:
                #  用户名登陆
                user = User.objects.get(username=username)
        except User.DoesNotExist:
            return None
        # 校验用户密码
        if user is not None and user.check_password(password):
            return user

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None
